$(document).ready(function()
    {
        $.table_master = null;
        var selectID = null;
        
        $.init_master = function()
        {
            $.ajax({
                url: base_url + 'br-02/toko/get_all',
                type: 'post',
                success : function(data)
                {
                    $.table_master = $('#toko_master').dataTable({
                        "bJQueryUI": true,
                        "sPaginationType": "full_numbers",
                        "aaData": data.data,
                        "bDestroy": true,
                        "aoColumns":[
                                
                                    {'sTitle' : 'ID Toko', 'mDataProp' : 'ID_TOKO'},

                                    {'sTitle' : 'Nama', 'mDataProp' : 'NAMA_TOKO'},

                                    {'sTitle' : 'Alamat', 'mDataProp' : 'ALAMAT'},

                                    {'sTitle' : 'Kota', 'mDataProp' : 'NAMA_KOTA'},

                                    {'sTitle' : 'Telpon', 'mDataProp' : 'TELPON'},

                                    {'sTitle' : 'Facimile', 'mDataProp' : 'FACIMILE'},

                                    {'sTitle' : 'Pimpinan', 'mDataProp' : 'PIMPINAN'},

                                    {'sTitle' : 'Kasir', 'mDataProp' : 'KASIR'},

                                    {'sTitle' : 'Ptg Pembelian', 'mDataProp' : 'PTG_PEMBELIAN'},

                                    {'sTitle' : 'Ptg terima Baranag', 'mDataProp' : 'PTG_TERIMA_BARANG'},

                                    {'sTitle' : 'Pilihan', 'mDataProp' : 'ID_TOKO'},
							
                            ],
                        "fnRowCallback": function( nRow, aData, iDisplayIndex )
                            {
                                $('td:last', nRow).html('<div style="text-align:center;"> <ul class="action-buttons clearfix"> \n\
                                                            <li> <a class="button button-gray no-text" href="#" onClick="$.update_toko_form(\''+ $('td:eq(0)', nRow).html() +'\')"> <span class="pencil"></span> </a> </li> \n\
                                                            <li> <a class="button button-gray no-text" href="#" onClick="$.delete_toko(\''+ $('td:eq(0)', nRow).html() +'\')"> <span class="bin"></span> </a> </li> \n\
                                                        </ul></div>');
                                return nRow;
                            }
                    });
                },
                error: function(res)
                {
                    alert(res.responseText);
                },
                dataType: 'json'
            });
        }
        $.init_master();

        $.add_toko = function()
        {
            $.call_ajax('br-02/toko/add/', "#toko_form", null);
            $('#dialog-toko-form').dialog('close');
            $.init_master();
        }

        $.update_toko = function()
        {
            $.call_ajax('br-02/toko/update/' + selectID, "#toko_form", null);
            $('#dialog-toko-form').dialog('close');
            $.init_master();
        }       

        $.delete_toko = function(id)
        {
            if(!confirm("Apakah anda yakin akan menghapus data ini (Semua data yang berhubungan dengan toko akan terhapus)?")) return false;
            $.call_ajax('br-02/toko/delete/' + id, null, null);
            $.init_master();
        }

        $('#dialog-toko-form').dialog({
            title: 'Data toko',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });

        $.update_toko_form = function(id)
        {
            $.call_ajax('br-02/toko/get/' + id, null, "#toko_form");
            selectID = id;
            $("#update_submit").show();
            $("#add_submit").hide();
            $('#dialog-toko-form').dialog('open');
        }

        $.add_toko_form = function()
        {
            $.clear_form("toko_form");

            $("#add_submit").show();
            $("#update_submit").hide();
            $('#dialog-toko-form').dialog('open');
        }

        $("#toko_form").submit(function() {return false;});

        $("#toko_add").click($.add_toko_form);
        $("#add_submit").click($.add_toko);
        $("#update_submit").click($.update_toko);
        
    });